﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mob___first_encounter
{
    class MobStandard : Mob, MobStatusChange
    {
        public MobStandard(int x, int y, Texture2D spriteTexture, Weapon weapon, int gameFPS)
        : base(x, y, spriteTexture, 1000, 6, 20, 0.8, weapon, 1, gameFPS)
        {
            attackAlarm = (int)(gameFPS / attackPerSecond);
        }

        public MobStandard(int x, int y, Texture2D spriteTexture, Weapon weapon)
        : base(x, y, spriteTexture, 1000, 6, 20, 0.8, weapon, 1)
        {
            attackAlarm = (int)(gameFPS / attackPerSecond);
	    }
	
	    public void Action(VampireRider player)
        {
            int playerX = player.GetX();
            int playerY = player.GetY();
            int moveX, moveY;
            int randomX, randomY;
            Random random = new Random();
            Vector2D vector;
            bool flag1, flag2;

            // alarm do ataku
            attackAlarm--;
            // odliczanie zakonczone
            if (attackAlarm == 0)
            {
                canAttack = true;
            }

            // hp = 0
            if (healthPoints == 0) return;

            // hp <= ~30%
            else if (healthPoints <= (maxHealthPoints / 10) * 2.5)
            {
                // odleglosc od gracza zbyt mala
                if (CheckDistanceFrom(playerX, playerY) <= 240)
                {
                    vector = new Vector2D(playerX, playerY, x, y);
                    vector.Normalize();
                }
                // na bezpiecznej odleglosci
                else
                {
                    do
                    {
                        randomX = random.Next(x - (int)(speed * 1.5), x + (int)(speed * 1.5));
                        randomY = random.Next(y - (int)(speed * 1.5), y + (int)(speed * 1.5));

                        vector = new Vector2D(playerX, playerY, randomX, randomY);

                        flag1 = vector.CalculateLength() >= 370;
                        flag2 = (randomX < x + (int)(speed * 0.8) && randomX > x - (int)(speed * 0.8)) || (randomY < y + (int)(speed * 0.8) && randomY > y - (int)(speed * 0.8));
                    } while (flag1 || flag2);
                    // przydalyby sie jeszcze warunki, zeby za plansze nie wychodzil (poza plansze) ale nie mam pojecia skad takie dane wziac..

                    vector = new Vector2D(x, y, randomX, randomY);
                    vector.Normalize();
                }
                moveX = x + (int)(vector.GetNormalizedX() * speed);
                moveY = y + (int)(vector.GetNormalizedY() * speed);

                MoveAt(moveX, moveY);
            }

            // hp > ~30%
            else
            {
                // poza zasiegiem strzalu
                if (CheckDistanceFrom(playerX, playerY) >= 130)
                {
                    vector = new Vector2D(x, y, playerX, playerY);
                    vector.Normalize();

                    moveX = x + (int)(vector.GetNormalizedX() * speed);
                    moveY = y + (int)(vector.GetNormalizedY() * speed);

                    MoveAt(moveX, moveY);
                }

                // w zasiegu strzalu
                else
                {
                    // atak
                    if (canAttack)
                    {
                        bool isHit = false;
                        int missX, missY;

                        if (random.NextDouble() <= accuracy) isHit = true;

                        // celny strzal
                        if (isHit) AttackAt(playerX, playerY);

                        // niecelny strzal
                        else
                        {
                            if (random.Next(0, 2) == 0)
                            {
                                missX = playerX + random.Next(playerX + 1, (int)(playerX + 1 + CheckDistanceFrom(playerX, playerY) / 20 + (1 - accuracy) * 20));
                                missY = playerY + random.Next(playerY + 1, (int)(playerY + 1 + CheckDistanceFrom(playerX, playerY) / 20 + (1 - accuracy) * 20));
                            }
                            else
                            {
                                missX = playerX - random.Next(playerX + 1, (int)(playerX + 1 + CheckDistanceFrom(playerX, playerY) / 20 + (1 - accuracy) * 20));
                                missY = playerY - random.Next(playerY + 1, (int)(playerY + 1 + CheckDistanceFrom(playerX, playerY) / 20 + (1 - accuracy) * 20));
                            }

                            AttackAt(missX, missY);
                        }

                        canAttack = false;
                        attackAlarm = (int)(gameFPS / attackPerSecond);
                    }

                    // dodatkowe akcje

                    do
                    {
                        randomX = random.Next(x - (int)(speed * 0.75), x + (int)(speed * 0.75));
                        randomY = random.Next(y - (int)(speed * 0.75), y + (int)(speed * 0.75));

                        vector = new Vector2D(playerX, playerY, randomX, randomY);

                        flag1 = vector.CalculateLength() <= 110;
                        flag2 = (randomX < x + (int)(speed * 0.4) && randomX > x - (int)(speed * 0.4)) || (randomY < y + (int)(speed * 0.4) && randomY > y - (int)(speed * 0.4));
                    } while (flag1 || flag2);

                    vector = new Vector2D(x, y, randomX, randomY);
                    vector.Normalize();

                    moveX = x + (int)(vector.GetNormalizedX() * speed * 0.5);
                    moveY = y + (int)(vector.GetNormalizedY() * speed * 0.5);

                    MoveAt(moveX, moveY);
                }
            }
        }
	
	    public void ReduceHealth(int points)
        {
            ChangeHealth(healthPoints + armor - points);
        }

    }
}
